//
//  BaocuoMenuView.m
//  ebook
//
//  Created by 吴超飞 on 16/3/9.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "BaocuoMenuView.h"
#import "UIImage+RTTint.h"
#import "UIPopoverListView.h"
#import "TranscodingView.h"

#define ViewWidth 110//视图宽度
#define CellHeight 40//单元格高度
#define CellCount 5//单元格个数

@interface BaocuoMenuView()<UIPopoverListViewDataSource, UIPopoverListViewDelegate,TranscodingViewDelegate>

@end

@implementation BaocuoMenuView

-(instancetype)initWithFrame:(CGRect)frame{
    
    frame.size.width = ViewWidth;
    frame.size.height = CellCount*CellHeight;
    self = [super initWithFrame:frame];
    self.backgroundColor = MTConfigColor(@"MTReaderToolBar_Bg_Color");
    
    self.layer.cornerRadius = 5;
    
    if (self) {
        
        UIImageView *arrowIv = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width - 30, -8, 14, 10)];
        _arrowIv = arrowIv;
        arrowIv.image = [[UIImage imageNamed:@"more_popver_arrow"] rt_tintedImageWithColor:MTConfigColor(@"MTReaderToolBar_Bg_Color")];
        arrowIv.contentMode = UIViewContentModeBottom;
        [self addSubview:arrowIv];
        
        _btnArray = [NSMutableArray array];
        
        UIButton *btn4 = [self buttonWithFrame:CGRectMake(0, 0, ViewWidth, CellHeight) title:@" 书籍详情" imageName:@"baocuo_icon4_nl" tag:4 withLine:YES];
        [self addSubview:btn4];
        [_btnArray addObject:btn4];
        
        UIButton *btn1 = [self buttonWithFrame:CGRectMake(0, CellHeight, ViewWidth, CellHeight) title:@" 刷新本章" imageName:@"baocuo_icon3_nl" tag:1 withLine:YES];
        [self addSubview:btn1];
        [_btnArray addObject:btn1];
        
        UIButton *btn2 = [self buttonWithFrame:CGRectMake(0, 2*CellHeight, ViewWidth, CellHeight) title:@" 章节报错" imageName:@"baocuo_icon2_nl" tag:3 withLine:YES];
        [self addSubview:btn2];
        [_btnArray addObject:btn2];
        
        UIButton *btn3 = [self buttonWithFrame:CGRectMake(0, 3*CellHeight, ViewWidth, CellHeight) title:@" 更新太慢" imageName:@"baocuo_icon_nl" tag:2 withLine:YES];
        [self addSubview:btn3];
        [_btnArray addObject:btn3];
        
        UIButton *btn5 = [self buttonWithFrame:CGRectMake(0, 4*CellHeight, ViewWidth, CellHeight) title:@" 转码声明" imageName:@"baocuo_icon5_nl" tag:5 withLine:NO];
        [self addSubview:btn5];
        [_btnArray addObject:btn5];
        
    }
    return self;
}

-(UIButton *)buttonWithFrame:(CGRect)frame title:(NSString *)title imageName:(NSString *)imgName tag:(NSInteger)tag withLine:(BOOL)hasLine{
    
    UIButton *iconBtn = [[UIButton alloc] initWithFrame:frame];
    iconBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    iconBtn.tag = tag;
    [iconBtn setTitle:title forState:UIControlStateNormal];
    [iconBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIColor *textColor = MTConfigColor(_isNight?@"MTReaderBar_NightNomarlText_Color":@"MTReaderBar_DayNomarlText_Color");
    [iconBtn setImage:[[UIImage imageNamed:imgName] rt_tintedImageWithColor:textColor] forState:UIControlStateNormal];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(10, CellHeight-1, frame.size.width-10, 0.5)];
    line.backgroundColor = MTConfigColor(@"MTReaderTopBar_baocuoLineBg_Color");
    if(hasLine)
        [iconBtn addSubview:line];
    
    return iconBtn;
}

-(void)btnClick:(UIButton *)btn{
    
    if (btn.tag==3) {
        UIPopoverListView *poplistview = [[UIPopoverListView alloc] initWithFrame:CGRectMake(10, 200, MTScreenW*0.55, 44*5+kPopoverListViewTitleHeight)];
        poplistview.delegate = self;
        poplistview.datasource = self;
        poplistview.listView.scrollEnabled = FALSE;
        [poplistview setTitle:@"请选择错误类型"];
        [poplistview show];
    }else if(btn.tag==2){// 更新太慢
        [self selectContent:btn.tag];
    }else if(btn.tag == 1){//刷新章节
        if ([self.delegate respondsToSelector:@selector(baocuoMenuView:refreshChapterBtnclick:)]) {
            [self.delegate baocuoMenuView:self refreshChapterBtnclick:btn];
        }
    }else if(btn.tag == 4){//转到书城
        if ([self.delegate respondsToSelector:@selector(baocuoMenuView:redirectStoreBtnclick:)]) {
            [self.delegate baocuoMenuView:self redirectStoreBtnclick:btn];
        }
    }else if(btn.tag == 5){//转码声明
        
        TranscodingView *transcoding = [[TranscodingView alloc] init];
        transcoding.delegate = self;
        [transcoding show];
    }
}

- (void)transcodingView:(TranscodingView *)transcodingView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if ([self.delegate respondsToSelector:@selector(baocuoMenuView:clickedTranscodingButtonAtIndex:)]) {
        [self.delegate baocuoMenuView:self clickedTranscodingButtonAtIndex:buttonIndex];
    }
}


-(void)selectContent:(NSInteger)content{
    
    if ([self.delegate respondsToSelector:@selector(baocuoMenuView:selectContent:)]) {
        [self.delegate baocuoMenuView:self selectContent:content];
    }
}

-(void)setIsNight:(BOOL)isNight{
    
    _isNight = isNight;
    UIColor *textColor = MTConfigColor(_isNight?@"MTReaderBar_NightNomarlText_Color":@"MTReaderBar_DayNomarlText_Color");
    
    for (UIButton *btn in _btnArray) {
        UIImage *img = btn.imageView.image;
        [btn setImage:[img rt_tintedImageWithColor:textColor] forState:UIControlStateNormal];
        [btn setTitleColor:textColor forState:UIControlStateNormal];
    }
    
    self.backgroundColor = MTConfigColor(isNight?@"MTReaderToolBar_Bg_Color":@"MTReaderToolBar_DayBg_Color");
    _arrowIv.image = [[UIImage imageNamed:@"more_popver_arrow"] rt_tintedImageWithColor:self.backgroundColor];
}

#pragma mark - UIPopoverListViewDataSource

- (UITableViewCell *)popoverListView:(UIPopoverListView *)popoverListView
                    cellForIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    
    NSInteger row = indexPath.row;
    NSArray *errorArray = @[@"排版混乱",@"重复章节",@"章节缺失",@"章节乱码"];
    if(indexPath.section==0&&row<4){
        cell.textLabel.text = errorArray[row];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
    }else if(indexPath.section==1){
        cell.textLabel.text = @"关闭";
        cell.textLabel.textColor = [UIColor lightGrayColor];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return cell;
}

- (CGFloat)popoverListView:(UIPopoverListView *)popoverListView
   heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (NSInteger)popoverListView:(UIPopoverListView *)popoverListView
       numberOfRowsInSection:(NSInteger)section
{
    return section==0?4:1;
}

#pragma mark - UIPopoverListViewDelegate
- (void)popoverListView:(UIPopoverListView *)popoverListView
     didSelectIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section==0){
        [self selectContent:(3+indexPath.row)];
    }
}

@end
